from pptx import Presentation
import os
# 使用python-pptx库来读取ppt文件并获取里面的图片和视频文件->pip install python-pptx
# 读取ppt文件


# 读取ppt文件
ppt_file = '实验ppt.pptx'
pr = Presentation(ppt_file)

# 创建img文件夹
output_folder = 'img'
if not os.path.exists(output_folder):
    os.makedirs(output_folder)

# 遍历每一页
for slide in pr.slides:
    for shape in slide.shapes:
        if shape.shape_type == 13:  # 图片类型
            image = shape.image
            image_bytes = image.blob
            with open(os.path.join(output_folder, f'image_{slide.slide_id}_{shape.shape_id}.png'), 'wb') as f:
                f.write(image_bytes)

    for slide_id, slide in enumerate(pr.slides, start=1):
        for shape_id, shape in enumerate(slide.shapes, start=1):
            if shape.has_text_frame:
                if 'video' in shape.text:
                    video = shape.text_frame
                    for paragraph in video.paragraphs:
                        for run in paragraph.runs:
                            if 'file://' in run.text:
                                video_path = run.text.split('file://')[1].strip()
                                with open(video_path, 'rb') as video_file:
                                    video_bytes = video_file.read()
                                    with open(os.path.join(output_folder, f'video_{slide_id}_{shape_id}.mp4'), 'wb') as f:
                                        f.write(video_bytes)

print('图片和视频文件已保存到img文件夹中')
